'use strict';
const db = uniCloud.database();
const collection = db.collection('books');
const booksType = db.collection('books-type');
exports.main = async (event, context) => {
	let {
		name
	} = event
	let res = {}
	switch (event.action) {
		case 'view':
			res = await collection.where({
				_id: event.id
			}).get()
			break
		case 'save':
			if (event.id) {
				res = await collection.doc(event.id).set(event)
			} else {
				res = await collection.add(event)
			}
			break
		case 'typeList':
			res = await booksType.orderBy("_id", "desc").get()
			break
		case 'list':
			if (event.type) {
				res = await collection.where({
					type: event.type
				}).orderBy("create_date", "desc").get()
			} else {
				res = await collection.orderBy("create_date", "desc").get()
			}
			break
		case 'delete':
			res = await collection.where({
				_id:event.id
			}).remove()
			// res = event.id
			break
	}
	//返回数据给客户端
	return res
};
